const mysql = require("mysql");

class UserService {
    constructor(pool) {
        this.pool = pool;
    }
    queryAll(response) {
        let sql = 'select * from user';
        this.pool.getConnection((err, connection) => {
            if(err){
                throw err;
            }
            connection.query(sql, (err, results) => {
                if (err) {
                    console.log(err.getMessage());
                } else {
                    let jsonObject = {
                        result: 'ok',
                        users: results
                    };
                    response.end(JSON.stringify(jsonObject));
                }
                connection.release();
            });
        });
    }
    login(response,params){
        let sql = "select count(*) as number from user where name=? and password=?";
        let json = null;
        this.pool.getConnection((error,connection)=>{
            if(error){
                throw error;
            }
            connection.query(sql,params,(error,results)=>{
                if(results[0].number==1){
                    //登录成功
                    json = {
                        result:'ok',
                        message:'登录成功'
                    }
                }else{
                    json = {
                        result:'error',
                        message:'用户名或密码有误！'
                    }
                }
                response.end(JSON.stringify(json));
                connection.release();
            });
        });
    }
    checkUser(response, urlParam) {
        if (urlParam.query) {
            if (urlParam.query.includes("=")) {
                let userName = urlParam.query.split('=')[1]; //提取浏览器提交上来的数据
                let sql = "select count(*) as number from user where name=?";
                this.pool.getConnection((error, connection) => {
                    if (error) {
                        throw error;
                    }
                    let json = null;
                    connection.query(sql, userName, (error, results) => {
                        if (error) {
                            console.log(error);
                            json = {
                                result: 'error',
                                message: '查询错误'
                            }
                        } else {
                            if (results[0].number == 0) {
                                json = {
                                    result: 'ok',
                                    data: true, //true：用户名不冲突，false：用户名冲突
                                };
                            } else {
                                json = {
                                    result: 'ok',
                                    data: false
                                }
                            }
                        }
                        response.end(JSON.stringify(json));
                        connection.release();
                    });
                });
            }else{
                response.end(JSON.stringify({result:'error',message:'参数错误'}));
            }
        }else{
            response.end(JSON.stringify({result:'error',message:'参数错误'}));
        }
    }
}
module.exports = UserService;